package nl.ruudshuis.data;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nl.ruudshuis.beans.GebruikerBean;
import nl.ruudshuis.dao.GebruikersDao;
import nl.ruudshuis.db.DBmanager;

/**
 * Servlet implementation class LoginController
 */
public class LoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String item = request.getParameter("submit");
		
		if(item != null){
			if(item.equals("Uitloggen")){
				HttpSession session = request.getSession();
				session.invalidate();
				
				response.sendRedirect("VeilingController");
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String email = request.getParameter("login");
		String pass = request.getParameter("pass");
		
		GebruikersDao g = new GebruikersDao((DBmanager)getServletContext().getAttribute("dbman"));
		GebruikerBean b = g.checkLogin(email, pass);
		
		HttpSession session = request.getSession();
		if(b != null){
			session.setAttribute("loggedIn", b);
			request.setAttribute("combinatie", "gelukt");
		}
		else{
			session.setAttribute("loggedIn", null);
			request.setAttribute("combinatie", "verkeerd");
		}
		
		response.sendRedirect("VeilingController");
		
		
	}

}
